class Fetcher 
  def self.logger
    @@logger ||= Logger.new(File.join(RAILS_ROOT,"log","fetcher.log"))
  end

  def logger
    self.class.logger
  end

  def fetch_matches
    begin
      puts "start fetch matches"
      logger.info "#{Time.now} start fetch matches"
      matches_count = Sport.fetch_matches
      logger.info "#{matches_count} matches was fetched."
      puts "#{matches_count} matches was fetched"
    rescue Exception=>e
      puts e.message
      puts e.backtrace.join("\n")
      logger.error e.message
      logger.error e.backtrace.join("\n")
    end
  end

  def fetch_results
    begin
      puts "start fetch results"
      logger.info "#{Time.now} start fetch results"
      matches = Sport.fetch_results
      logger.info "#{matches.size} was played."
      puts "#{matches.size} was processed."
      next_match = Match.next_match
      logger.info "next : #{next_match.info}"
      puts "next: #{next_match.info}"
    rescue Exception=>e
      puts e.message
      puts e.backtrace.join("\n")
      logger.error e.message
      logger.error e.backtrace.join("\n")
    end
  end

  def self.start
    puts "Fether.start"
    fetcher = self.new
    puts "Fetcher.new"
    fetcher.fetch_matches
    fetcher.fetch_results
  end
end
